package com.jnu.myaccount;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jnu.myaccount.data.Data;
import com.jnu.myaccount.data.DataBank.DataBank;
import com.jnu.myaccount.data.Money;
import com.jnu.myaccount.function.home.HomeFragment;
import com.jnu.myaccount.record.IncomeFragment;
import com.jnu.myaccount.record.PaidFragment;

import java.io.Serializable;
import java.util.ArrayList;

public class DetailActivity extends AppCompatActivity implements View.OnClickListener{
    private ImageView back;//返回键
    private TextView delete;//删除键
    private TextView edit;//修改键
    private TextView name;//记账名字
    private TextView cost;//记账金额
    private TextView date;//记账日期
    private TextView time;//记账时间
    private TextView mark;//记账金额
    private TextView type;//记账类型
    private ImageView image;//类型图片
    private int position;//点击的位置


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        back=findViewById(R.id.show_lv_back);
        back.setOnClickListener(this);
        delete=findViewById(R.id.show_delete);
        delete.setOnClickListener(this);
        edit=findViewById(R.id.show_edit);
        edit.setOnClickListener(this);
        //绑定控件
        name=findViewById(R.id.show_type_name);
        cost=findViewById(R.id.show_cost);
        date=findViewById(R.id.show_date);
        time=findViewById(R.id.show_time);
        mark=findViewById(R.id.show_mark);
        type=findViewById(R.id.type);

        image=findViewById(R.id.show_type_iv);
        Bundle bundle = getIntent().getExtras();//在这里去处理传过来的数据
        if(bundle!=null) {
            Serializable serializable = bundle.getSerializable("DataInfo");
            if (serializable != null && serializable instanceof Data) {
                //goodsEntityList.add(new Data("111",20.0,"none",new Date(),R.drawable.home));
                Data g = (Data) serializable;//获取此条item的数据
                position= bundle.getInt("position");
                if (bundle.getString("type").equals("show"))//修改数据
                {
                    name.setText(g.itemName);
                    cost.setText(g.itemCost+"元");
                    date.setText(g.mDate);
                    time.setText(g.mTime);
                    mark.setText(g.itemMark);
                    int ImageID;
                    if(g.itemCost<=0)//paid
                    {
                        type.setText("支出");
                        for(int i=0;i< PaidFragment.itemNames.length;i++)
                            if(PaidFragment.itemNames[i].equals(g.itemName)){
                                ImageID=PaidFragment.itemImage[i];
                                image.setImageResource(ImageID);
                                break;
                            }
                    }
                    else{
                        type.setText("收入");
                        for(int i=0;i< IncomeFragment.itemNames_Income.length;i++)
                            if(IncomeFragment.itemNames_Income[i].equals(g.itemName)){
                                ImageID=IncomeFragment.itemImage_Income[i];
                                image.setImageResource(ImageID);
                                break;
                            }
                    }

                }

            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.show_lv_back:
                this.finish();
                break;

            case R.id.show_delete:
                AlertDialog.Builder alertDB = new AlertDialog.Builder(this);
                alertDB.setPositiveButton(this.getResources().getString(R.string.string_confirmation), (dialogInterface, i) -> {
                    DataBank mDataBank=new DataBank(this);
                    ArrayList<Data> goodsEntityList=mDataBank.loadData();
                    Data mData=goodsEntityList.get(position);
                    goodsEntityList.remove(position);
                    HomeFragment.itemAllMoney-=mData.itemCost;
                    if(mData.itemCost>0){
                        HomeFragment.itemAllEarn-=mData.itemCost;
                    }
                    else{
                        HomeFragment.itemAllPaid-=mData.itemCost;
                    }
                    Money mMoney=new Money(HomeFragment.itemAllMoney,HomeFragment.itemAllPaid,HomeFragment.itemAllEarn);
                    mDataBank.saveMoney(mMoney);
                    mDataBank.saveData(goodsEntityList);
                    Intent intent=new Intent(this, MainActivity.class);
                    startActivity(intent);
                });
                alertDB.setNegativeButton(this.getResources().getString(R.string.string_cancel), (dialogInterface, i) -> {

                });
                alertDB.setMessage(this.getResources().getString(R.string.string_confirm_delete) +name.getText()+"？");
                alertDB.setTitle(this.getResources().getString(R.string.hint)).show();
                break;

            case R.id.show_edit:
                Intent intent=new Intent(this,RecordActivity.class);
                Bundle bundle=new Bundle();
                DataBank mDataBank=new DataBank(this);
                ArrayList<Data> goodsEntityList=mDataBank.loadData();
                Data mDate=goodsEntityList.get(position);
                bundle.putSerializable("DataInfo",mDate);
                bundle.putInt("position",position);
                if(mDate.itemCost<=0){
                    bundle.putString("type","Paid");//这条额外的信息标识这是一条支出--修改信息
                    HomeFragment.itemAllPaid=HomeFragment.itemAllPaid-mDate.itemCost;
                }
                else{
                    bundle.putString("type","Income");//这条额外的信息标识这是一条收入--修改信息
                    HomeFragment.itemAllEarn=HomeFragment.itemAllEarn-mDate.itemCost;
                }
                Log.i("position1",position+"");
                HomeFragment.itemAllMoney=HomeFragment.itemAllMoney-mDate.itemCost;
                intent.putExtras(bundle);
                startActivity(intent);
        }
    }
}